package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.Expert;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ExpertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class ExpertService {

    @Autowired
    private ExpertMapper expertMapper;

    
    public Integer insert(Expert expert){
        expert.setStatus(BaseStatusEnum.INVALID.getValue());
        expertMapper.insert(expert);
        return expert.getId();
    }


    public int update(Expert expert){
        if(expert.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return expertMapper.updateByPrimaryKeySelective(expert);
    }


    public int delete(Integer id){
        return expertMapper.deleteByPrimaryKey(id);
    }

    public Expert selectById(Integer id){
        return expertMapper.selectByPrimaryKey(id);
    }

    public List<Expert> selectByParams(String name, Integer status){
        return expertMapper.selectByParams(name, status);
    }

    /**
     * 切换发布状态
     * @param id
     * @return
     */
    public int updateStatus(Integer id){
        Expert expert = expertMapper.selectByPrimaryKey(id);
        if(expert.getStatus().equals(BaseStatusEnum.VALID.getValue())){
            expert.setStatus(BaseStatusEnum.INVALID.getValue());
        } else {
            expert.setStatus(BaseStatusEnum.VALID.getValue());
        }
        return expertMapper.updateByPrimaryKeySelective(expert);
    }

    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return expertMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return expertMapper.batchDelete(dto.getIds());
    }
}
